package org.xyl.game.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.xyl.game.entity.Userinfo;
import org.xyl.game.service.RoomService;
import org.xyl.game.service.UserInfoService;
import org.xyl.game.tool.GsonUtil;
import org.xyl.game.tool.serializer.UserinfoSerializer;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@Controller
public class SigninController {
    @Autowired
    private UserInfoService userInfoService;

    @PostMapping(path = "/signin", produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String signin(String username,String password){
        Userinfo userinfo = userInfoService.addUserinfo(username, password);
        Map<String ,String> ansMap=new HashMap<>();
        if(userinfo==null){
            ansMap.put("state","用户已存在");
            return GsonUtil.getGsonBuilder().create().toJson(ansMap);
        }
        userinfo.setPassword("");
        String ret= GsonUtil.getGsonBuilder().registerTypeAdapter(Userinfo.class,new UserinfoSerializer()).create().toJson(userinfo);
        return ret;

    }
}
